﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultiDAQ_Analysis.View
{
    public partial class CompactMessageBox : Form
    {
        public enum MessageType
        {
            Success,
            Warning,
            Error
        }

        private CompactMessageBox(string message, string title, MessageType type)
        {
            InitializeComponent();
            InitializeForm(message, title, type);
        }

        private void InitializeForm(string message, string title, MessageType type)
        {
            // 基本窗体设置
            this.Text = title;
            this.Size = new Size(280, 150);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.BackColor = Color.White;
            this.Padding = new Padding(10);

            // 创建控件
            var icon = new PictureBox
            {
                Size = new Size(32, 32),
                Location = new Point(20, 25),
                SizeMode = PictureBoxSizeMode.StretchImage
            };

            var label = new Label
            {
                Text = message,
                Location = new Point(65, 25),
                Size = new Size(190, 40),
                Font = new Font(this.Font.FontFamily, 9),
                TextAlign = ContentAlignment.MiddleLeft,
                AutoSize = false
            };

            var btnOk = new Button
            {
                Text = "确定",
                Size = new Size(75, 30),
                Location = new Point((this.ClientSize.Width - 75) / 2, this.ClientSize.Height - 40),
                DialogResult = DialogResult.OK,
                FlatStyle = FlatStyle.Flat,
                Font = new Font(this.Font.FontFamily, 9)
            };
            btnOk.FlatAppearance.BorderSize = 0;

            // 根据消息类型设置样式
            switch (type)
            {
                case MessageType.Success:
                    icon.Image = SystemIcons.Information.ToBitmap();
                    label.ForeColor = Color.FromArgb(76, 175, 80);
                    btnOk.BackColor = Color.FromArgb(76, 175, 80);
                    break;
                case MessageType.Warning:
                    icon.Image = SystemIcons.Warning.ToBitmap();
                    label.ForeColor = Color.FromArgb(255, 152, 0);
                    btnOk.BackColor = Color.FromArgb(255, 152, 0);
                    break;
                case MessageType.Error:
                    icon.Image = SystemIcons.Error.ToBitmap();
                    label.ForeColor = Color.FromArgb(244, 67, 54);
                    btnOk.BackColor = Color.FromArgb(244, 67, 54);
                    break;
            }

            btnOk.ForeColor = Color.White;

            // 添加控件到窗体
            this.Controls.Add(icon);
            this.Controls.Add(label);
            this.Controls.Add(btnOk);

            // 设置接受按钮
            this.AcceptButton = btnOk;

            // 确保窗体在最前面显示
            this.TopMost = true;
        }

        // 静态方法用于显示不同类型的弹窗
        public static DialogResult ShowSuccess(string message, string title = "提示")
        {
            using (var form = new CompactMessageBox(message, title, MessageType.Success))
            {
                return form.ShowDialog();
            }
        }

        public static DialogResult ShowWarning(string message, string title = "警告")
        {
            using (var form = new CompactMessageBox(message, title, MessageType.Warning))
            {
                return form.ShowDialog();
            }
        }

        public static DialogResult ShowError(string message, string title = "错误")
        {
            using (var form = new CompactMessageBox(message, title, MessageType.Error))
            {
                return form.ShowDialog();
            }
        }

        // 重写OnLoad以调整窗体大小
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // 根据文本内容自动调整高度
            using (var g = this.CreateGraphics())
            {
                var textSize = g.MeasureString(this.Controls[1].Text, this.Controls[1].Font, this.Controls[1].Width);
                if (textSize.Height > 40)
                {
                    this.Height = (int)(this.Height + (textSize.Height - 40));
                    this.Controls[2].Location = new Point(
                        (this.ClientSize.Width - this.Controls[2].Width) / 2,
                        this.ClientSize.Height - 40
                    );
                }
            }
        }
    }
}
